﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using FastMVVM.WP7.Template.ViewModel;
using FastMVVM.Core;

namespace FastMVVM.WP7.Template.View
{
    public partial class MainPageView : PhoneApplicationPage, IView<MainPageViewModel>
    {
        // The ViewModel should be the logic layer.
        // Use XAML Binding to access his properties.
        public MainPageViewModel ViewModel { get; set; }

        // The order of the calls need to be preservated.
        // Do not add any additional code to this, unless you really know what are you doing.
        // Any other initilization logic should be at the "PhoneApplicationPage_Loaded".
        public MainPageView()
        {
            ViewModel = ViewModelFactory.GetInstance<MainPageViewModel>();
            // if you don't want to share the same ViewModel with diferent Views (Portrait/Landscape) change to:
            //ViewModel = ViewModelFactory.New<MainPageViewModel>();

            SetViewModelEventHandlers();

            InitializeComponent();
        }

        // Occurs only after all the XAML's controls have been created.
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = ViewModel;

            ViewModel.LoadMessage();
        }

        // Occurs when the screen orientation change
        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if (e.Orientation == PageOrientation.LandscapeLeft
                || e.Orientation == PageOrientation.LandscapeRight)
            {
                NavigationService.Navigate(new Uri("/View/MainPageViewLandscape.xaml", UriKind.Relative));
            }
        }

        // Use to set the event handler to all the ViewModel's events.
        public void SetViewModelEventHandlers()
        {
            ViewModel.ShowMessage += ViewModel_ShowMessage;
        }

        private void ViewModel_ShowMessage(object sender, EventArgs e)
        {
            sbShowMessage.Begin();
        }
    }
}